﻿#region License
// MIT License
//
// Copyright(c) 2019-2025 ZhangYu
// https://gitee.com/zhangyu800/litebyte
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#endregion
#region Intro
// Purpose: Reflection Optimize Performance
// Author: ZhangYu
// CreateDate: 2019-11-22
// LastModifiedDate: 2025-02-19
#endregion
// 使用代理优化反射
#define USE_DELEGATE_REFLECTION
// 使用指针优化反射
#define USE_POINTER_REFLECTION
namespace LiteByte.Reflection {

    using System.Reflection;

    /// <summary> 类成员包装器(用于对象成员的取值或赋值) </summary>
    public interface IMemberWrapper {

        /// <summary> 获取成员的类型 </summary>
        Type Type { get; }
        /// <summary> 获取成员的值 </summary>
        object GetValue(object instance);
        /// <summary> 设置成员的值 </summary>
        void SetValue(object instance, object value);

    }

    /// <summary> 类成员包装器(用于对象字段或属性的取值或赋值) </summary>
    public class MemberWrapper<T, V> : IMemberWrapper {

        public Type Type { get; }
        private bool IsField { get; }
        private IFieldWrapper fieldWrapper;
        private IPropertyWrapper propertyWrapper;

        public MemberWrapper(MemberInfo memberInfo) {
            if (memberInfo is FieldInfo) {
                IsField = true;
                FieldInfo fieldInfo = (FieldInfo)memberInfo;
                Type = fieldInfo.FieldType;
                #if USE_POINTER_REFLECTION
                fieldWrapper = new FieldPointerWrapper(fieldInfo);
                #else
                fieldWrapper = new LBFieldReflectionWrapper(fieldInfo);
                #endif
            } else if (memberInfo is PropertyInfo) {
                IsField = false;
                PropertyInfo propertyInfo = (PropertyInfo)memberInfo;
                Type = propertyInfo.PropertyType;
                #if USE_DELEGATE_REFLECTION
                propertyWrapper = new PropertyDelegateWrapper<T, V>(propertyInfo);
                #else
                propertyWrapper = new LBPropertyReflectionWrapper(propertyInfo);
                #endif
            } else {
                throw new Exception("MemberInfo error! MemberInfo is not FieldInfo or PropertyInfo");
            }
        }

        public object GetValue(object instance) {
            if (IsField) return fieldWrapper.GetValue(instance);
            return propertyWrapper.GetValue(instance);
        }

        public void SetValue(object instance, object value) {
            if (IsField) {
                fieldWrapper.SetValue(instance, value);
            } else {
                propertyWrapper.SetValue(instance, value);
            }
        }

    }

}